#include <allegro.h>
#include <stdio.h>
#include "car.h"

/**
* @file  car.c
* @author  Fendri Med Omat
* @version 1.0
* @date 23 May 2013
* @brief Define the different movements of the car
*/





/**
* @fn  int get_pixel(BITMAP* Buffer,int x,int y))
* @details  This function gives the exact pixel code.
* @param    The buffer that loads the image
	    * x is a variable that indicates a position
	    * y is a variable that idicates a position 
	   
* @return   void */
int get_pixel(BITMAP* Buffer,int x,int y)
	{
		int tmp;
		tmp=getpixel(Buffer,x,y);
		return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
	}


/**
* @fn  void moveup (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
* @details  Car's Up-movement 
* @param   b is a pointer on the structure BACKG
	   *c is a pointer on the structure BITMAP
	   *The buffer that loads the image
	   *image is a pointer on BITMAP
* @return   void */
void moveup (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
	{
		b->backg_x-=1; //speed of the background
		b->backgc_x-=1;
		c->y-=1; //speed of the car

		clear_bitmap(buffer) ;
		draw_sprite (buffer,b->backgroundprincipale,b->backg_x,b->backg_y) ;
		blit (c->car,image,0,0,0,0,260,290) ;
		draw_sprite (buffer,image,c->x,c->y) ;
		blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	}

/**
* @fn  void movedown (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
* @details  Car's down-movement 
* @param   b is a pointer on the structure BACKG
	   *c is a pointer on the structure BITMAP
	   *The buffer that loads the image
	   *image is a pointer on BITMAP
* @return   void */
void movedown (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
	{
		b->backg_x-=1;
		b->backgc_x-=1;
		c->y+=1;
		rest(0) ;
		clear_bitmap(buffer) ;




		draw_sprite (buffer,b->backgroundprincipale,b->backg_x,b->backg_y) ;
		blit (c->car,image,0,0,0,0,260,290) ;
		draw_sprite (buffer,image,c->x,c->y) ;
		blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	}

/**
* @fn  void moveright (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
* @details  Car's right-movement 
* @param   b is a pointer on the structure BACKG
	   *c is a pointer on the structure BITMAP
	   *The buffer that loads the image
	   *image is a pointer on BITMAP
* @return   void */
void moveright (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
	{
		b->backg_x-=2;
  		b->backgc_x-=2;
		c->x+=2;
		rest(0) ;

		clear_bitmap(buffer) ;
		draw_sprite (buffer,b->backgroundprincipale,b->backg_x,b->backg_y) ;
		blit (c->car,image,0,0,0,0,260,290) ;
		draw_sprite (buffer,image,c->x,c->y) ;
		blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	}

/**
* @fn  void moveleft (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
* @details  Car's left-movement 
* @param   b is a pointer on the structure BACKG
	   *c is a pointer on the structure BITMAP
	   *The buffer that loads the image
	   *image is a pointer on BITMAP
* @return   void */
void moveleft (BACKG *b, CAR *c, BITMAP *buffer, BITMAP * image)
	{
		b->backg_x-=1;
		b->backgc_x-=1;
		c->x-=2;
		rest(0) ;
		clear_bitmap(buffer) ;


		draw_sprite (buffer,b->backgroundprincipale,b->backg_x,b->backg_y) ;
		blit (c->car,image,0,0,0,0,260,290) ;
		draw_sprite (buffer,image,c->x,c->y) ;
		blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	}


void soundm(sound)
	{
		/////////////////**Sound**////////////
		if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) 
		{
		allegro_message("Sound Error: %s", allegro_error);
		}
		///////*Volume*//////////////
		set_volume(255, 255); //digital and music to max levels
		//////////////////////*Load*////////////////////////////////
		sound = load_sample("/home/omar/1234/1.wav");
		////////////////*Play*////////////////////////////////////////
		play_sample(sound, 255, 128, 1000, 0);
		if (!sound)
		{
		allegro_message("Error reading wav file"); 
		}
	}
